<?php

class Packet extends CActiveRecord
{
	/**
	 * The followings are the available columns in table '{{packet}}':
	 * @var integer $id
	 * @var string $name
	 * @var string $payment_sum
	 * @var string $traffic_sum
	 * @var boolean $is_unlim
	 * @var integer $default_speed_id
	 * @var string $world_cost
	 * @var string $ukraine_cost
	 * @var string $world_night_cost
	 * @var string $ukraine_night_cost
	 * @var boolean $is_active
	 * @var string $create_time
	 * @var string $update_time
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{packet}}';
	}
	
	public function scopes()
	{
		return array(
			'active'=>array(
				'condition'=>'t.is_active=1',
				'order'=>'t.id',
			),
				//'withspeed'=>array(
				//'with'=>'speed',
			//),
		);
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, payment_sum, traffic_sum, is_unlim, default_speed_id, world_cost, ukraine_cost, world_night_cost, ukraine_night_cost', 'required'),
			array('default_speed_id', 'numerical', 'integerOnly'=>true),
			array('payment_sum, traffic_sum, world_cost, ukraine_cost, world_night_cost, ukraine_night_cost', 'numerical'),
			array('name', 'length', 'max'=>128),
			array('is_active', 'boolean'),
			array('create_time, update_time', 'unsafe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, payment_sum, traffic_sum, is_unlim, default_speed_id, world_cost, ukraine_cost, world_night_cost, ukraine_night_cost, is_active, create_time, update_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'speed'=>array(self::BELONGS_TO,'Speed','default_speed_id',
					'select'=>'id,name',),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('main','Id'),
			'name' => Yii::t('main','Name'),
			'payment_sum' => Yii::t('main','Payment Sum'),
			'traffic_sum' => Yii::t('main','Traffic Sum'),
			'is_unlim' => Yii::t('main','Is Unlim'),
			'default_speed_id' => Yii::t('main','Default Speed'),
			'world_cost' => Yii::t('main','World Cost'),
			'ukraine_cost' => Yii::t('main','Ukraine Cost'),
			'world_night_cost' => Yii::t('main','World Night Cost'),
			'ukraine_night_cost' => Yii::t('main','Ukraine Night Cost'),
			'is_active' => Yii::t('main','Is Active'),
			'create_time' => Yii::t('main','Create Time'),
			'update_time' => Yii::t('main','Update Time'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('name',$this->name,true);

		$criteria->compare('payment_sum',$this->payment_sum,true);

		$criteria->compare('traffic_sum',$this->traffic_sum,true);

		$criteria->compare('is_unlim',$this->is_unlim);

		$criteria->compare('default_speed_id',$this->default_speed_id);

		$criteria->compare('world_cost',$this->world_cost,true);

		$criteria->compare('ukraine_cost',$this->ukraine_cost,true);

		$criteria->compare('world_night_cost',$this->world_night_cost,true);

		$criteria->compare('ukraine_night_cost',$this->ukraine_night_cost,true);

		$criteria->compare('is_active',$this->is_active);

		$criteria->compare('create_time',$this->create_time,true);

		$criteria->compare('update_time',$this->update_time,true);

		return new CActiveDataProvider('Packet', array(
			'criteria'=>$criteria,
			'pagination'=>array('pageSize'=>Setting::get('PerPage')),
		));
	}
	
	public function behaviors()
    {
        return array(
			'History' => array(
                'class'=>'application.components.behaviors.HistoryBehavior',
            ),
			'OldAttributes' => array(
				'class'=>'application.components.behaviors.OldAttributesBehavior',
			),
            'NotActiveOnDelete' => array(
            	'class'=>'application.components.behaviors.NotActiveOnDeleteBehavior',
            ),
        );
    }
}